﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLibs;
using System.Data;

namespace MDF.DataHelper.DataObject.UsersDO
{
    public class UserMembershipDAO : BaseDataObject
    {
        #region Constructor

        public UserMembershipDAO()
        {
        }

        public UserMembershipDAO(IData _objData)
        {
            this.m_objData = _objData;
        }
        #endregion

        public DataTable GetByUsername(string _strUsername)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserMembership_Username]");
                this.m_objData.AddParameter("@Username", _strUsername);
                dtResult=this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public void Insert(int UserId, string Password, string PasswordSalt, bool IsConfirmed)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserMembership_Insert]");
                this.m_objData.AddParameter("@UserId", UserId);
                this.m_objData.AddParameter("@Password", Password);
                this.m_objData.AddParameter("@PasswordSalt", PasswordSalt);
                this.m_objData.AddParameter("@IsConfirmed", IsConfirmed);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public void Update(int UserId, string Password, string PasswordSalt)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserMembership_Update]");
                this.m_objData.AddParameter("@UserId", UserId);
                this.m_objData.AddParameter("@Password", Password);
                this.m_objData.AddParameter("@PasswordSalt", PasswordSalt);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }

        public DataTable GetByUserID(int UserID)
        {
            DataTable dtResult = new DataTable();
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserMembership_UserID]");
                this.m_objData.AddParameter("@UserID", UserID);
                dtResult=this.m_objData.ExecStoreToDataTable();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
            return dtResult;
        }

        public void UpdateActive(int UserID, bool IsConfirm, int UpdatedUser)
        {
            try
            {
                this.Connect();
                this.m_objData.CreateNewStoredProcedure("[UserMembership_UpdateActive]");
                this.m_objData.AddParameter("@UserID", UserID);
                this.m_objData.AddParameter("@IsConfirm", IsConfirm);
                this.m_objData.AddParameter("@UpdatedUser", UpdatedUser);
                this.m_objData.ExecNonQuery();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                this.DeConnect();
            }
        }
    }
}
